package com.weilele.leisure.time.ui.holder


import android.graphics.drawable.GradientDrawable
import android.view.View
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.databinding.HolderPlayListItemBinding
import com.weilele.mvvm.adapter.MvvmHolder
import com.weilele.mvvm.adapter.mvvmRcvAdapter
import com.weilele.mvvm.utils.activity.dp
import com.weilele.mvvm.utils.activity.getResColor
import com.weilele.mvvm.utils.activity.textFrom


class PlayListItemHolder :
    MvvmHolder<VideoDetailBean.PlayersDTO.DatasDTO, HolderPlayListItemBinding> {
    companion object {
        fun newInstance() = mvvmRcvAdapter(PlayListItemHolder::class.java, ::onSame, ::onSame)
        private fun onSame(
            old: VideoDetailBean.PlayersDTO.DatasDTO?,
            new: VideoDetailBean.PlayersDTO.DatasDTO?
        ): Boolean {
            return old?.playUrl == new?.playUrl
                    && old?.text == new?.text
                    && old?.isSelect == new?.isSelect
        }
    }

    constructor(binding: HolderPlayListItemBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    private val selectBg by lazy {
        GradientDrawable().apply {
            setColor(getResColor(R.color.colorAccent))
            cornerRadius = 2f.dp
        }
    }
    private val unSelectBg by lazy {
        GradientDrawable().apply {
            setColor(getResColor(R.color.windowBackground))
            cornerRadius = 2f.dp
        }
    }

    override fun bindData(data: VideoDetailBean.PlayersDTO.DatasDTO) {
        mBinding.root.textFrom(data.text)
        if (data.isSelect) {
            mBinding.root.background = selectBg
            mBinding.root.setTextColor(getResColor(android.R.color.white))
        } else {
            mBinding.root.setTextColor(getResColor(R.color.colorAppText))
            mBinding.root.background = unSelectBg
        }
    }
}